package com.zyh.homework.test1;

/**
 * @Author zyh
 * @Date 2024/8/7 17:00
 * @Description 1.    设计一个多线程的程序如下：设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出，现在有5个售票点同时售票，
 * 用5个线程模拟这5个售票点的售票情况。
 */

public class Test1 {
    public static void main(String[] args) {
        MyTask01 myTask01 = new MyTask01();
        Thread t1 = new Thread(myTask01, "售票点1");
        t1.start();
        Thread t2 = new Thread(myTask01, "售票点2");
        t2.start();
        Thread t3 = new Thread(myTask01, "售票点3");
        t3.start();
        Thread t4 = new Thread(myTask01, "售票点4");
        t4.start();
        Thread t5 = new Thread(myTask01, "售票点5");
        t5.start();
    }
}

class MyTask01 implements Runnable {
    private int num = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (num <= 0) {
                    break;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在卖第：" + num + "张票");
                num--;
            }
        }
    }
}